package com.example.zhang;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

public class Select extends AppCompatActivity implements Runnable {
    private static final String TAG = "a";
    private static final String APP_ID = "20210607000856459";
    private static final String SECURITY_KEY = "j3yx0pqj2Af3z6KUFVJq";
    private EditText input;
    private TextView output;
    private String ch = null;
    private  String en =null;
    private Handler handler;


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        input = findViewById(R.id.input_trans);
        output = findViewById(R.id.result);

    }

    public void translate(View v){
        ch = input.getText().toString();
        if(ch==null)
            output.setText("");
        else{
        Thread t = new Thread((Runnable) this);//开启后会启动子线程中的run方法.
        t.start();
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg){
                if(msg.what==1){
                    String ks = (String)msg.obj;
                    output.setText(ks);
                }
                super.handleMessage(msg);
            }
        };}
//        SharedPreferences sharedPreferences = getSharedPreferences("content", Activity.MODE_PRIVATE);
//        PreferenceManager.getDefaultSharedPreferences(this);
//        en = sharedPreferences.getString("out","123");
//        output.setText(en);

    }

    public void run(){
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        String query = ch;
        String str = api.getTransResult(query, "auto", "en");
        try {
            JSONObject jsonObject = new JSONObject(str);
            String out = jsonObject.getString("trans_result");
            out = out.substring(1,out.length()-1);
            JSONObject js = new JSONObject(out);
            String out1 = js.getString("dst");
            Log.i(TAG,out1);

            SharedPreferences sp = getSharedPreferences("content",Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("out",out1);
            editor.apply();

            Message msg = handler.obtainMessage(1);
            msg.obj = out1;
            handler.sendMessage(msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}